package com.hx.xiao.controller;


import com.hx.xiao.entity.Fan;
import com.hx.xiao.service.IFanService;
import com.hx.xiao.utils.Code;
import com.hx.xiao.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/piyaoh/fan")
public class FanController {
    @Autowired
    private IFanService fanService;



    @PostMapping("/add")
    public R addById(@RequestBody Fan fan){
        int i = fanService.addById(fan);
        if (i>0){
            return new R(Code.GET_OK,fan,"添加成功");
        }else {
            return new R(Code.GET_ERRO,"添加失败");
        }
    }

    @GetMapping("/find/{uid}")
    public R findAll(@PathVariable Integer uid){
        List<Fan> all = fanService.findAll(uid);
        if (all!=null){
            return new R(Code.GET_OK,all,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
    @GetMapping("/all")
    private R findAllBy(){
        List<Fan> allBy = fanService.findAllBy();
        if (allBy!=null){
            return new R(Code.GET_OK,allBy,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    @GetMapping("/se/{id}")
    private R findById(@PathVariable Integer id){
        Fan byId = fanService.findById(id);
        if(byId!=null){
            return new R(Code.GET_OK,byId,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    @GetMapping("/del/{id}")
    private R deleteById(@PathVariable Integer id){
        int i = fanService.DeleteById(id);
        if (i>0){
            return new R(Code.DELETE_OK,"删除成功");
        }else {
            return new R(Code.DELETE_ERRO,"删除失败");
        }
    }

    @PostMapping("/up")
    private R updateByIdq(@RequestBody Fan fan){
        int i = fanService.updateByIdi(fan);
        if (i>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new  R(Code.UPDATE_ERRO,"修改失败");
        }
    }
}
